package com.yswee.framework.widget.pulltorefresh;

import com.yswee.framework.R;
import com.yswee.framework.widget.RotateView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;


public class PullToRefreshHeader extends PullToRefreshHeaderBase {
	
	static final int DEFAULT_ROTATION_ANIMATION_DURATION = 150;

	protected ImageView headerImage;
	protected RotateView headerProgress;
	protected TextView headerText;

	protected String pullLabel;
	protected String refreshingLabel;
	protected String releaseLabel;

	protected Animation rotateAnimation; 
	protected Animation resetRotateAnimation;

	public PullToRefreshHeader(Context context) {
		super(context);
		initialize(PullToRefreshBase.MODE_PULL_UP_TO_REFRESH);
	}
	
	public PullToRefreshHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		initialize(PullToRefreshBase.MODE_PULL_UP_TO_REFRESH);
	}
	
	public PullToRefreshHeader(Context context, int mode) {
		super(context);
		initialize(mode);
	}
	
	protected void initialize(int mode) {
		ViewGroup header = (ViewGroup) LayoutInflater.from(getContext()).inflate(R.layout.widget_pulltorefresh_header, this);
		headerText = (TextView) header.findViewById(R.id.pull_to_refresh_text);
		headerImage = (ImageView) header.findViewById(R.id.pull_to_refresh_image);
		headerProgress = (RotateView) header.findViewById(R.id.pull_to_refresh_progress);
	
		final Interpolator interpolator = new LinearInterpolator();
		rotateAnimation = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
		        0.5f);
		rotateAnimation.setInterpolator(interpolator);
		rotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
		rotateAnimation.setFillAfter(true);
	
		resetRotateAnimation = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f,
		        Animation.RELATIVE_TO_SELF, 0.5f);
		resetRotateAnimation.setInterpolator(interpolator);
		resetRotateAnimation.setDuration(DEFAULT_ROTATION_ANIMATION_DURATION);
		resetRotateAnimation.setFillAfter(true);
	
		this.releaseLabel = getResources().getString(R.string.widget_pulltorefresh_release_label);
		switch (mode) {
			case PullToRefreshBase.MODE_PULL_UP_TO_REFRESH:
				this.pullLabel = getResources().getString(R.string.widget_pulltorefresh_pullup_label);
				break;
			case PullToRefreshBase.MODE_PULL_DOWN_TO_REFRESH:
			default:
				this.pullLabel = getResources().getString(R.string.widget_pulltorefresh_pulldown_label);
				break;
		}
		this.refreshingLabel = getResources().getString(R.string.widget_pulltorefresh_refreshing_label);
	
		switch (mode) {
			case PullToRefreshBase.MODE_PULL_UP_TO_REFRESH:
				headerImage.setImageResource(R.drawable.widget_pulltorefresh_up_arrow);
				break;
			case PullToRefreshBase.MODE_PULL_DOWN_TO_REFRESH:
			default:
				headerImage.setImageResource(R.drawable.widget_pulltorefresh_down_arrow);
				break;
		}
	}

	@Override
	protected void reset() {
		headerText.setText(pullLabel);
		headerImage.setVisibility(View.VISIBLE);
		headerProgress.setVisibility(View.GONE);
	}

	@Override
	protected void pullToRefresh() {
		headerText.setText(pullLabel);
		headerImage.clearAnimation();
		headerImage.startAnimation(resetRotateAnimation);
	}

	@Override
	protected void releaseToRefresh() {
		headerText.setText(releaseLabel);
		headerImage.clearAnimation();
		headerImage.startAnimation(rotateAnimation);
	}

	@Override
	protected void refreshing() {
		headerText.setText(refreshingLabel);
		headerImage.clearAnimation();
		headerImage.setVisibility(View.INVISIBLE);
		headerProgress.setVisibility(View.VISIBLE);
	}

	@Override
	protected void setPullLabel(String pullLabel) {
		this.pullLabel = pullLabel;
	}

	@Override
	protected void setReleaseLabel(String releaseLabel) {
		this.releaseLabel = releaseLabel;
	}

	@Override
	protected void setRefreshingLabel(String refreshingLabel) {
		this.refreshingLabel = refreshingLabel;
	}

}
